$(function () {
    layui.config({
        base: '/Public/'
    }).extend({
        formSelects: 'layui-formSelects/src/formSelects-v4'
    });

    layui.use(['form','formSelects'], function () {
        var form = layui.form(),
            formSelects = layui.formSelects;

        //渲染数据评审角色
        formSelects.data('reviewRole[1]', 'local', {
            arr: [
                {"name": "销售总监", "value": 1},
                {"name": "研发总监", "value": 2},
                {"name": "产品经理", "value": 3},
                {"name": "结构经理", "value": 4},
                {"name": "项目管理员", "value": 5}
            ]
        });
        //渲染数据server
        /*formSelects.data('reviewRole', 'server', {  //'local' | 'server', 本地数据或者远程数据
            url: "",
            beforeSuccess: function(id, url, searchVal, result){
                //我要把数据外层的code, msg, data去掉
                result = result.data;
                //我要反转name
                /!*$.each(result, function(index, item) {
                    item.name && (item.name = item.name.split('').reverse().join(''))
                });*!/
                //然后返回数据
                return result;
            },
            success: function(id, url, searchVal, result){
                console.log('id: example6_3, 成功返回数据!!!');
            }
        });*/

        //渲染数据抄送角色
        formSelects.data('ccRole', 'local', {
            arr: [
                {"name": "销售总监", "value": 1},
                {"name": "研发总监", "value": 2},
                {"name": "产品经理", "value": 3},
                {"name": "结构经理", "value": 4},
                {"name": "项目管理员", "value": 5}
            ]
        });

        //评审级别增加生成的html
        function createReview(number) {
            var html = '<div class="layui-form-item">\n' +
                '            <input type="text" class="layui-input input-label" name="">\n' +
                '            <div class="layui-input-block">\n' +
                '                <div class="review-content">\n' +
                '                    <div class="review-content-select">\n' +
                '                        <div class="review-content-item">\n' +
                '                            <select class="reviewType" name="" lay-verify="required">\n' +
                '                                <option value="">请选择评审类型</option>\n' +
                '                                <option value="1">或签</option>\n' +
                '                                <option value="2">会签</option>\n' +
                '                            </select>\n' +
                '                        </div>\n' +
                '                        <div class="review-content-item">\n' +
                '                            <select class="reviewRole" name="" xm-select="reviewRole['+ number + ']" xm-select-search="" xm-select-search-type="title" lay-verify="required">\n' +
                '                                <option value="">请选择评审角色</option>\n' +
                '                            </select>\n' +
                '                        </div>\n' +
                '                    </div>\n' +
                '                    <div>\n' +
                '                        <button type="button" class="layui-btn layui-btn-primary" data-action="add" title="增加评审级别">\n' +
                '                            <i class="icon-plus"></i>\n' +
                '                        </button>\n' +
                '                        <button type="button" class="layui-btn layui-btn-primary" data-action="cut" title="删除评审级别">\n' +
                '                            <i class="icon-minus"></i>\n' +
                '                        </button>\n' +
                '                    </div>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '        </div>';

            return html;
        }

        var number = 1;
        //评审级别的点击增加删除按钮触发事件
        $('.review').on('click', 'button',function(){

            //增加评审级别
            if ($(this).attr('data-action') == 'add') {
                number++;
                $('.review').append(createReview(number));
                //重新渲染评审类型
                form.render();
                //重新渲染评审人员
                formSelects.render("reviewRole["+ number + "]");
                formSelects.data("reviewRole["+ number + "]", 'local', {
                    arr: [
                        {"name": "销售总监", "value": 1},
                        {"name": "研发总监", "value": 2},
                        {"name": "产品经理", "value": 3},
                        {"name": "结构经理", "value": 4},
                        {"name": "项目管理员", "value": 5}
                    ]
                });
            }

            //删除评审级别
            if ($(this).attr('data-action') == 'cut') {
                $(this).parents('.layui-form-item').remove();
            }

            //循环赋值索引
            //name
            $(".input-label").each(function (n) {
                n= n+1;
                $(this).val(n+"级评审");
                $(this).attr("name","reviewRules["+ n +"][name]");
            });

            //type
            $(".reviewType").each(function (n) {
                n = n+1;
                $(this).attr("name","reviewRules["+ n +"][type]");
            });

            //role
            $(".reviewRole").each(function (n) {
                n = n+1;
                $(this).attr("name","reviewRules["+ n +"][role]");
                $(this).parent().find('.xm-hide-input').attr("name","reviewRules["+ n +"][role]");
            });

        });

        //监听创建评审规则
        form.on('submit(create)', function (data) {
            console.log(data.field);
            $.post('/Home/Posts/post', data.field, function (response) {
                if (response.code == 0) {

                } else {
                    layer.msg(response.msg, {icon:2});
                }
            }, 'json')
        })
    })
});